package com.cy.detectsys.controller.azure;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.detectsys.entity.*;
import com.cy.detectsys.mapper.*;
import com.cy.detectsys.util.R;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@RestController
public class azureController {

    @Resource
    private CaidaAsnMapper caidaAsnMapper;
    @Resource
    private AsTopoMapper asTopoMapper;

    @RequestMapping("azure/asn/point")
    public R asnPoint() {
        List<CaidaAsn> caidaAsns = caidaAsnMapper.selectList(null);

//        新增一个重点asn点
        List<Long> numbers = new ArrayList<>();

        // 添加数字到列表中
        numbers.add(8075L);
        numbers.add(8068L);
        numbers.add(8069L);
        numbers.add(8070L);
        numbers.add(3598L);
        numbers.add(200517L);
        numbers.add(53587L);
        numbers.add(59067L);
        numbers.add(63314L);
        numbers.add(45139L);
        numbers.add(58862L);
        numbers.add(35106L);
        numbers.add(23468L);
        numbers.add(12076L);



        caidaAsns.stream().filter(asn -> numbers.contains(asn.getAsn())).forEach(item -> item.setImportant(true));

        return R.ok(caidaAsns).put("importantAsns", numbers);
     }

    @RequestMapping("azure/asn/connect")
    public R asnPointConnect() {
        List<AsTopo> asTopos = asTopoMapper.selectList(null);
        return R.ok(asTopos);
    }


    @Resource
    private CableInfoMapper cableInfoMapper;
    /**
     * 光缆数据入库
     */
    @RequestMapping("cable/data/process")
    public R dataProcess() {
        File folder = new File("E:\\微软云\\cable\\cableInfo");
        for (File file : folder.listFiles()) {

            String readString = FileUtil.readString(file, Charset.defaultCharset());
//            CableInfo cableInfo = JSON.parseObject(readString.replaceAll("id", "cableId"), CableInfo.class);
//            System.out.println(cableInfo);
//            cableInfoMapper.insert(cableInfo);

            JSONObject jsonObject = JSON.parseObject(readString.replaceAll("id", "cableId"));
            jsonObject.getJSONArray("landing_points");
        }


        return R.ok();
    }


    @Resource
    private CableLandingPointMapper cableLandingPointMapper;

    //插入着陆点
    @RequestMapping("cable/point/insert")
    public R cablepointinsert() {
        File landingpoint = new File("E:\\微软云\\cable\\geoInfo\\landing-point-geo.json");

        String readString2 = FileUtil.readString(landingpoint, Charset.defaultCharset());

        System.out.println(readString2);


        JSONObject parse = JSON.parseObject(readString2);
        JSONArray landingpointList = parse.getJSONArray("features");
        for (Object o : landingpointList) {
            JSONObject jsonObject = (JSONObject) o;
            JSONObject properties = jsonObject.getJSONObject("properties");
            String id = properties.getString("id");
            String name = properties.getString("name");

            JSONObject geometry = jsonObject.getJSONObject("geometry");
            JSONArray coordinates = geometry.getJSONArray("coordinates");


            CableLandingPoint cableLandingPoint = new CableLandingPoint();
            cableLandingPoint.setLandingPointId(id);
            cableLandingPoint.setLandingPointName(name);
            cableLandingPoint.setLatitude((BigDecimal) coordinates.get(1));
            cableLandingPoint.setLongitude((BigDecimal) coordinates.get(0));
            cableLandingPointMapper.insert(cableLandingPoint);
        }
        return R.ok();

    }


    @Resource
    private CableLandingPointAssociationMapper cableLandingPointAssociationMapper;
    // 找到光缆和着陆点的关系
    @RequestMapping("cable/point/association")
    public R findAssociation() {

        File folder = new File("E:\\微软云\\cable\\cableInfo");
        for (File file : folder.listFiles()) {

            String readString = FileUtil.readString(file, Charset.defaultCharset());
//

            JSONObject jsonObject = JSON.parseObject(readString);
            String cableId = jsonObject.getString("id");

            JSONArray landingPoints = jsonObject.getJSONArray("landing_points");
            for (Object landingPoint : landingPoints) {
                JSONObject landingP = (JSONObject) landingPoint;
                String landingPId = landingP.getString("id");
                System.out.println(landingPId);
                QueryWrapper<CableLandingPoint> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("landing_point_id", landingPId);
                CableLandingPoint cableLandingPoint = cableLandingPointMapper.selectOne(queryWrapper);
                System.out.println(cableLandingPoint);

                QueryWrapper<CableInfo> cableInfoQueryWrapper = new QueryWrapper<>();
                cableInfoQueryWrapper.eq("cable_id", cableId);

                CableInfo cableInfo = cableInfoMapper.selectOne(cableInfoQueryWrapper);
                try {
                    CableLandingPointAssociation cableLandingPointAssociation = new CableLandingPointAssociation();
                    cableLandingPointAssociation.setCableId(cableInfo.getId());
                    cableLandingPointAssociation.setLandingPointId(cableLandingPoint.getId());
                    cableLandingPointAssociationMapper.insert(cableLandingPointAssociation);
                } catch (Exception e) {
                    System.out.println();
                }

            }

        }

        return R.ok();
    }


    //绘制着陆点
    @RequestMapping("getLandingPoint")
    public R getLandingPoint() {
        PageHelper.startPage(1, 10);

        List<CableLandingPoint> cableLandingPoints = cableLandingPointMapper.selectList(null);
        return R.ok(cableLandingPoints);
    }


    @RequestMapping("getCable")
    public R getCable() {
        PageHelper.startPage(1, 10);
        List<CableInfo> cableInfos = cableInfoMapper.selectList(null);
        Long l = cableInfoMapper.selectCount(null);
        return R.ok(cableInfos).put("cableNum", l);
    }


    @RequestMapping("getCurrentCable")
    public R getCable(@RequestParam Integer id) {
        CableInfo cableInfo = cableInfoMapper.selectById(id);
        return R.ok(cableInfo);
    }


    // 插入 光缆折线段
    @RequestMapping("cable/point/line/segment")
    public R cablepointsegment() {
        File landingpoint = new File("E:\\微软云\\cable\\geoInfo\\cable-geo.json");

        String readString2 = FileUtil.readString(landingpoint, Charset.defaultCharset());

        System.out.println(readString2);


        JSONObject parse = JSON.parseObject(readString2);

        for (Object o : parse.getJSONArray("features")) {

            JSONObject landingP = (JSONObject) o;
            String cableid = landingP.getJSONObject("properties").getString("id");
            String color = landingP.getJSONObject("properties").getString("color");

            JSONArray jsonArray = landingP.getJSONArray("geometry").getJSONObject(0).getJSONArray("coordinates");
            QueryWrapper<CableInfo> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("cable_id", cableid);
            CableInfo cableInfo = cableInfoMapper.selectOne(objectQueryWrapper);
            try {
                cableInfo.setCoordinates(JSON.toJSONString(jsonArray));
                cableInfo.setColor(color);
                cableInfoMapper.updateById(cableInfo);
            } catch (Exception e) {
                System.out.println();
            }

        }

        return R.ok();

    }
}
